/* jcifs smb client library in Java
 * Copyright (C) 2000  "Michael B. Allen" <jcifs at samba dot org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package jcifs.netbios;

import java.io.IOException;
import java.io.InputStream;

/**
 * Abstract base class for NetBIOS session service packets.
 * This class provides common functionality for session layer communication.
 */
public abstract class SessionServicePacket {

    /**
     * Default constructor for SessionServicePacket.
     * Creates a new session service packet instance.
     */
    public SessionServicePacket() {
        // Default constructor
    }

    // session service packet types
    static final int SESSION_MESSAGE = 0x00;
    static final int SESSION_REQUEST = 0x81;

    /**
     * NetBIOS positive session response packet type.
     */
    public static final int POSITIVE_SESSION_RESPONSE = 0x82;

    /**
     * NetBIOS negative session response packet type.
     */
    public static final int NEGATIVE_SESSION_RESPONSE = 0x83;
    static final int SESSION_RETARGET_RESPONSE = 0x84;
    static final int SESSION_KEEP_ALIVE = 0x85;

    static final int MAX_MESSAGE_SIZE = 0x0001FFFF;
    static final int HEADER_LENGTH = 4;

    static void writeInt2(final int val, final byte[] dst, int dstIndex) {
        dst[dstIndex] = (byte) (val >> 8 & 0xFF);
        dstIndex++;
        dst[dstIndex] = (byte) (val & 0xFF);
    }

    static void writeInt4(final int val, final byte[] dst, int dstIndex) {
        dst[dstIndex] = (byte) (val >> 24 & 0xFF);
        dstIndex++;
        dst[dstIndex++] = (byte) (val >> 16 & 0xFF);
        dst[dstIndex++] = (byte) (val >> 8 & 0xFF);
        dst[dstIndex] = (byte) (val & 0xFF);
    }

    static int readInt2(final byte[] src, final int srcIndex) {
        return ((src[srcIndex] & 0xFF) << 8) + (src[srcIndex + 1] & 0xFF);
    }

    static int readInt4(final byte[] src, final int srcIndex) {
        return ((src[srcIndex] & 0xFF) << 24) + ((src[srcIndex + 1] & 0xFF) << 16) + ((src[srcIndex + 2] & 0xFF) << 8)
                + (src[srcIndex + 3] & 0xFF);
    }

    static int readLength(final byte[] src, int srcIndex) {
        srcIndex++;
        return ((src[srcIndex++] & 0x01) << 16) + ((src[srcIndex++] & 0xFF) << 8) + (src[srcIndex++] & 0xFF);
    }

    static int readn(final InputStream in, final byte[] b, final int off, final int len) throws IOException {
        int i = 0, n;

        while (i < len) {
            n = in.read(b, off + i, len - i);
            if (n <= 0) {
                break;
            }
            i += n;
        }

        return i;
    }

    static int readPacketType(final InputStream in, final byte[] buffer, final int bufferIndex) throws IOException {
        int n = readn(in, buffer, bufferIndex, HEADER_LENGTH);
        if (n != HEADER_LENGTH) {
            if (n == -1) {
                return -1;
            }
            throw new IOException("unexpected EOF reading netbios session header");
        }
        return buffer[bufferIndex] & 0xFF;
    }

    int type, length;

    /**
     * Writes the packet to the specified byte array in wire format.
     *
     * @param dst the destination byte array to write to
     * @param dstIndex the starting index in the destination array
     * @return written bytes
     */
    public int writeWireFormat(final byte[] dst, final int dstIndex) {
        this.length = writeTrailerWireFormat(dst, dstIndex + HEADER_LENGTH);
        writeHeaderWireFormat(dst, dstIndex);
        return HEADER_LENGTH + this.length;
    }

    int readWireFormat(final InputStream in, final byte[] buffer, final int bufferIndex) throws IOException {
        readHeaderWireFormat(in, buffer, bufferIndex);
        return HEADER_LENGTH + readTrailerWireFormat(in, buffer, bufferIndex);
    }

    int writeHeaderWireFormat(final byte[] dst, int dstIndex) {
        dst[dstIndex] = (byte) this.type;
        dstIndex++;
        if (this.length > 0x0000FFFF) {
            dst[dstIndex] = (byte) 0x01;
        }
        dstIndex++;
        writeInt2(this.length, dst, dstIndex);
        return HEADER_LENGTH;
    }

    int readHeaderWireFormat(final InputStream in, final byte[] buffer, int bufferIndex) {
        this.type = buffer[bufferIndex] & 0xFF;
        bufferIndex++;
        this.length = ((buffer[bufferIndex] & 0x01) << 16) + readInt2(buffer, bufferIndex + 1);
        return HEADER_LENGTH;
    }

    abstract int writeTrailerWireFormat(byte[] dst, int dstIndex);

    abstract int readTrailerWireFormat(InputStream in, byte[] buffer, int bufferIndex) throws IOException;
}
